//
//  SaveViewController.m
//  Motor
//
//  Created by 戎耀 on 15-8-26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SaveViewController.h"
#import "PrefixHeader.pch"
#import "DataBaseHandle.h"
#import "CollcetModel.h"
#import "SaveContentViewController.h"
#import <RESideMenu.h>
#import "SaveTableViewCell.h"
@interface SaveViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageView1;
@end

@implementation SaveViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self dataHandl];
    SelfNavBar.hidden = YES;
    [self.tableView reloadData];
    if (self.collec.count == 0) {
        [UIView animateWithDuration:1 animations:^{
            
            [self.view addSubview:self.imageView1];
        }];
    }

}

#pragma mark --数据处理
- (void)dataHandl
{
    self.collec = [NSMutableArray array];
    [[DataBaseHandle shareDataBaseHandle] open];
    self.collec = (NSMutableArray *)[[DataBaseHandle shareDataBaseHandle] selecttCollectWithAll];
    
    [[DataBaseHandle shareDataBaseHandle] close];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(ScreenWidth * 5 / 375, ScreenHeight * 35 / 667, ScreenWidth * 60 / 375, ScreenWidth * 45 / 667);
    backButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:RGB(22, 188, 180) forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    [backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self tableViewSubView];
    
    self.imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * 85 / 375, ScreenHeight * 200 / 667, ScreenWidth * 180 / 375, ScreenHeight * 140 / 667)];
    self.imageView1.image = [UIImage imageNamed:@"meitianjia.png"];
    
}

#pragma mark --返回按键方法
- (void)backButtonClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark --tableView初始化
- (void)tableViewSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, ScreenHeight * 80 / 667, ScreenWidth, ScreenHeight - ScreenHeight * 80 / 667) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[SaveTableViewCell class] forCellReuseIdentifier:@"reuse"];
}
#pragma mark --cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenHeight * 100 / 667;
    
}

#pragma mark --cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collec.count;
    
}
#pragma mark --cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollcetModel *model = [[CollcetModel alloc] init];
    SaveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    model = [self.collec objectAtIndex:indexPath.row];
    cell.collectModel = model;
    return cell;
    
}

#pragma mark --点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SaveContentViewController *content = [[SaveContentViewController alloc] init];
    CollcetModel *model = [[CollcetModel alloc] init];
    model = [self.collec objectAtIndex:indexPath.row];

    content.webLink = model.url;
    content.title = model.title;
    
    [self presentViewController:content animated:YES completion:^{
        
    }];
}
#pragma mark --cell是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
    
}
#pragma mark --cell编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark --删除方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果是删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //首先编辑数据
        CollcetModel *collect = [[CollcetModel alloc] init];
        collect = [self.collec objectAtIndex:indexPath.row];
        [[DataBaseHandle shareDataBaseHandle] open];
        [[DataBaseHandle shareDataBaseHandle] deleteCollect:collect.title];
        self.collec = (NSMutableArray *)[[DataBaseHandle shareDataBaseHandle] selecttCollectWithAll];
        //编辑cell
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView reloadData];
        
    }
    
    if (self.collec.count == 0) {
        [UIView animateWithDuration:1 animations:^{
            
            [self.view addSubview:self.imageView1];
            
        }];
    }

}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
